//******************************************************************
//******************************************************************
//**********          ANts Peer To Peer Sources        *************
//
// ANts P2P realizes a third generation P2P net. It protects your
// privacy while you are connected and makes you not trackable, hiding
// your identity (ip) and crypting everything you are sending/receiving
// from others.

// Copyright (C) 2004  Roberto Rossi

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

package ants.p2p.gui;

import java.awt.Toolkit;
import java.awt.datatransfer.*;
import javax.swing.*;


public class QueryLinksDaemon extends Thread{
  GuiAnt caller;
  String lastLinkSeen = "";

  public static boolean monitorClipboard = true;

  public QueryLinksDaemon(GuiAnt caller){
    this.caller = caller;
    this.setPriority(1);
    this.start();
    while(!this.isAlive())
      try {
        Thread.currentThread().sleep(1000); //.yield()
      }
      catch (InterruptedException ex) {
      }
  }

  public void run(){
    while(true){
      try {
        this.sleep(3000);
        if(QueryLinksDaemon.monitorClipboard == false ||
           this.caller.cap.warriorAnt == null ||
           this.caller.cap.warriorAnt.isDisconnected() ||
           this.caller.cap.warriorAnt.getNeighboursNumber() == 0) continue;
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable link = clipboard.getContents(this);
        boolean foundMaterial = false;
        int selectedIndex = 0;
        for(int x = 0; x < link.getTransferDataFlavors().length; x++){
          if(link.getTransferDataFlavors()[x].isRepresentationClassCharBuffer()){
            foundMaterial = true;
            selectedIndex = x;
          }
        }
        if(!foundMaterial) continue;
        DataFlavor df = link.getTransferDataFlavors()[selectedIndex];
        Object clipboardContent = link.getTransferData(df);
        boolean ed2kFound = clipboardContent.toString().toLowerCase().split("\\x7c")[0].equals("ed2k://");
        boolean ants2kFound = clipboardContent.toString().toLowerCase().split("\\x7c")[0].equals("ants2k://");
        if ((ed2kFound || ants2kFound) &&
            !lastLinkSeen.equals(clipboardContent.toString())) {
          lastLinkSeen = clipboardContent.toString();
          FrameAnt.getInstance(null).transferFocus();
          int confirm = JOptionPane.showConfirmDialog(this.caller,
              ji.JI.i("Search the ANts Net for")+"\n" +
              clipboardContent.toString().toLowerCase().split("\\x7c")[2],
              ed2kFound ? ji.JI.i("ED2K Link in Clipboard") : ants2kFound ?
              ji.JI.i("ANts2K Link in Clipboard") : ji.JI.i("Link in Clipboard"),
              JOptionPane.OK_CANCEL_OPTION);
          if (confirm == JOptionPane.OK_OPTION) {
            final QueryAntPanel qap = new QueryAntPanel(this.caller);
            this.caller.queryPanels.add(qap);
            this.caller.jTabbedPane1.add(qap, new LabelIcon(ji.JI.i("Search"), DoubleBuffer.getInstance().getButton(12), qap));
            this.caller.cap.warriorAnt.getPropertyChangeSupport().
                addPropertyChangeListener(qap);
            qap.jTextField1.setText(clipboardContent.toString());
            Thread searchThread = new Thread() {
              public void run() {
                qap.searchByHash(true);
              }
            };
            searchThread.setPriority(10);
            searchThread.start();
          }
        }
      }
      catch (Exception ex) {
      }
    }
  }
}
